<?php 
/**
* school_admin入口
* 已作废
*/
class School_admin_class extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if(!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if($this->session->userdata('r_role_id') != 4)
            redirect('home');
    }

    /**
     * 学校管理员管理班级和专业的主页面
     * @return [type]
     */
    function index($active_tab='class_tab')
    {
        $school_id=$this->session->userdata('r_school_id');
        //准备数据
        $data = array();
        $data['title']='班级及专业管理';
        $this->load->model('major_model');
        $this->load->model('class_model');

        $data['majors']=$this->major_model->get_by_school_id($school_id);
        $data['classes']=$this->class_model->get_by_school_id($school_id);
        $data['active_tab']=$active_tab;
        $this->load->view('school_admin_class/index',$data);
    }
    /**
     * 查看一个班级的详细信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function class_check($id)
    {
        $data['title']='查看班级';
        $this->load->model('class_model');
        $class=$this->class_model->get_by_id($id);
        if(!$class) return View('error');

        $data['class']=$class;
        $data['students']=$this->class_model->get_students_from_class_id($class['id']);

        $this->load->view('school_admin_class/class_check',$data);
    }

    /**
     * 添加一个班级的信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function class_add()
    {
        //表单验证
        if($this->form_validation->run('school_admin_class/class_add'))
        {
            //post方法
            //读取数据
            $school_id=$this->session->userdata('r_school_id');
            $class_name=$this->input->post('class_name');

            $this->load->model('class_model');

            //检查如果这个班级名称已经存在，则提醒
            if($this->class_model->check_class_name_exist_in_a_school($class_name,$school_id))
            {
                $this->session->set_flashdata('message','这个班级已存在，添加失败');
                redirect('school_admin_class/class_add');
            }

            //新建一个
            $class = array(
                'name' => $class_name,
                'school_id' => $school_id
                 );

            $this->class_model->add($class);

            $this->session->set_flashdata('message','操作成功');
            redirect('school_admin_class/index');      
        }

        //get方法
        $data['title']='添加班级';
        $this->load->view('school_admin_class/class_add',$data);
    }


    /**
     * 删除一个班级
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function class_delete($id)
    {
        $this->load->model('class_model');
        if(!$this->class_model->get_by_id($id))
            return $this->load->view('error');
        if($this->class_model->delete($id))
            $this->session->set_flashdata('message','操作成功');
        else
            $this->session->set_flashdata('message','存在相关引用，操作失败');

        redirect('school_admin_class/index');

    }
    
    /**
     * 编辑一个班级
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function class_edit($id=0)
    {
        if($id)
        {
            //get方法
            $this->load->model('class_model');
            $class=$this->class_model->get_by_id($id);
            if(!$class)
                return $this->load->view('error');
            $data['title']='编辑班级信息';
            $data['class']=$class;

            
            $this->load->view('school_admin_class/class_edit',$data);
        }
        else
        {
            //post
            //读取数据
            $this->load->model('class_model');

            $id=$this->input->post('class_id');
            $class=$this->class_model->get_by_id($id);
            if(!$class)
                return $this->load->view('error');
            if($this->form_validation->run('school_admin_class/class_edit'))
            {
                //post方法
                $name=$this->input->post('class_name');
                
                //检查如果这个班级名称已经存在，则提醒
                if($this->class_model->check_class_name_exist_in_a_school($name,$school_id))
                {
                    $this->session->set_flashdata('message','这个班级已存在，操作失败');
                    redirect('school_admin_class/index');
                }

                //将内容写入数据库
                $item = array(
                    'name' => $name,
                     );
                
                //数据库操作
                if($this->class_model->update($id,$item))
                {
                    $this->session->set_flashdata('message','操作成功');
                    redirect('school_admin_class/index');  
                }
                else
                {
                    $this->session->set_flashdata('message','操作失败');
                    redirect('school_admin_class/index');  
                }
            }
            else
            {
                //验证失败
                //get方法
                $data['title']='编辑班级信息';
                $data['class']=$class;
                $this->load->view('school_admin_class/class_edit',$data);
            }
        }
        
        
    }
    /**
     * 添加一个专业
     * @return [type] [description]
     */
    function major_add()
    {
        //表单验证
        if($this->form_validation->run('school_admin_class/major_add'))
        {
            //post方法
            //读取数据
            $school_id=$this->session->userdata('r_school_id');
            $major_name=$this->input->post('major_name');

            $this->load->model('major_model');

            //检查如果这个专业名称已经存在，则提醒
            if($this->major_model->check_major_name_exist_in_a_school($major_name,$school_id))
            {
                $this->session->set_flashdata('message','这个专业已存在，添加失败');
                redirect('school_admin_class/major_add');
            }

            //新建一个
            $major = array(
                'name' => $major_name,
                'school_id' => $school_id
                 );

            $this->major_model->add($major);

            $this->session->set_flashdata('message','添加成功');
            redirect('school_admin_class/index/major_tab');        
        }

        //get方法
        $data['title']='添加专业';
        $this->load->view('school_admin_class/major_add',$data);
    }
    
    /**
     * 删除一个专业
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function major_delete($id)
    {
        $this->load->model('major_model');
        if(!$this->major_model->get_by_id($id))
            return $this->load->view('error');
        if($this->major_model->delete($id))
            $this->session->set_flashdata('message','操作成功');
        else
            $this->session->set_flashdata('message','存在相关引用，操作失败');

        redirect('school_admin_class/index/major_tab');

    }

    /**
     * 编辑一个专业
     * @param int $id
     * @param  [type] $id [description]
     * @return  [type]     [description]
     */
    function major_edit($id=0,$school_id)
    {
        if($id)
        {
            //get方法
            $this->load->model('major_model');
            $major=$this->major_model->get_by_id($id);
            if(!$major)
                return $this->load->view('error');
            $data['title']='编辑班级信息';
            $data['major']=$major;

            
            $this->load->view('school_admin_class/major_edit',$data);
        }
        else
        {
            //post
            //读取数据
            $this->load->model('major_model');

            $id=$this->input->post('major_id');
            $major=$this->major_model->get_by_id($id);
            if(!$major)
                return $this->load->view('error');


            if($this->form_validation->run('school_admin_class/major_edit'))
            {
                //post方法
                $name=$this->input->post('major_name');
                
                //检查如果这个名称已经存在，则提醒
                if($this->major_model->check_major_name_exist_in_a_school($name,$school_id))
                {
                    $this->session->set_flashdata('message','这个专业已存在，操作失败');
                    redirect('school_admin_class/index/major_tab');
                }

                //将内容写入数据库
                $item = array(
                    'name' => $name,
                     );
                
                //数据库操作
                if($this->major_model->update($id,$item))
                {
                    $this->session->set_flashdata('message','操作成功');
                    redirect('school_admin_class/index/major_tab');    
                }
                else
                {
                    $this->session->set_flashdata('message','操作失败');
                    redirect('school_admin_class/index/major_tab');    
                }
            }
            else
            {
                //验证失败
                //get方法
                $data['title']='编辑班级信息';
                $data['major']=$major;
                $this->load->view('school_admin_class/major_edit',$data);
            }
        }
        
        
    }
}